﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using System.Collections.Generic;
using Dict.Blo;
using UI;

public class AchievementItem : MonoBehaviour
{
    
    public GameObject goBtn;
    public GameObject receiveBtn;
    public GameObject finishBtn;

    public GameObject commonIcon;
    public UILabel curScore;
    public UIScrollView scrollView;
    public SimpleGrid grid;
    public UILabel title;
    public delegate void UpdateRedPoint();
    public UpdateRedPoint m_update_red_point;

    private string titleDesc    = "";
    private string rewardID     = "";
    private string pageName     = "";
    private string option       = "";
    public enum State
    {
        //0 可领取 1不满足 2 已经领取
        Finish  = 0,
        Doing   = 1,
        Receive = 2
    }
    public enum Type
    {
        VIP             = 1,
        PlyerLevel      = 2,
        PlayerLevel_2   = 3,
        Fuben           = 4,
        Fuben_2         = 5,
        Fight           = 6,//获得xxx战力
        Equip           = 7,//穿齐xx件装备
        Baowu           = 8,//上阵角色佩戴宝物数量
        ShiLian         = 9,//试炼
        Shangzhen       = 10,//上阵角色数
        TBC             = 11,//远征
        PanJun          = 12,//叛军
        TuPo            = 13,//角色突破
        Baodong         = 14,//暴动
        Buy             = 15,//限时优惠中购买道具次数
        Reset           = 16 //限时优惠重置次数     
    }

    public void InitItemWithInfo(AchievementInfo info)
    {
        foreach(var item in grid.childs)
        {
            Destroy(item);
        }
        grid.childs.Clear();
        goBtn.SetActive(false);
        finishBtn.SetActive(false);
        receiveBtn.SetActive(true);
        rewardID = info.id;
        curScore.text = CommonMethod.GetShowValue(info.currentNum) + "/" + CommonMethod.GetShowValue(info.totalNum);
        GetItemConfig(info.type);
        title.text = string.Format(TableTextBlo.GetText(titleDesc), CommonMethod.GetShowValue(info.totalNum));
        var rewards = info.reward.Split(new char[1] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);
        foreach(var reward in rewards)
        {
            
            var rewardInfo = reward.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
            GameObject go = GameObject.Instantiate(commonIcon) as GameObject;
            go.GetComponent<CommonIcon>().SetData(rewardInfo[0], true, int.Parse(rewardInfo[1]));
            go.transform.parent = grid.transform;
            go.transform.localPosition = Vector3.zero;
            go.transform.localScale = Vector3.one * 0.85f;
            //title
            grid.AddChild(go);           
        }
        if(rewards.Length <=3)
        {
            scrollView.enabled = false;
        }
        else
        {
            scrollView.enabled = true;
        }
        
        scrollView.ResetPosition();
        UpDataBtnState(info.state);
    }

    public  string GetShowValue(long v)
    {
        if (v > 9999)
        {            
            var a = v / 1000f;
            var _int = (int)a;
            return _int + TableTextBlo.GetText("wan");
        }
        else
        {
            return v.ToString();
        }
    }

    void UpDataBtnState(int s)
    {
        switch((State)s)
        {
            case State.Doing:
                goBtn.SetActive(true);
                finishBtn.SetActive(false);
                receiveBtn.SetActive(false);
                break;
            case State.Finish:
                goBtn.SetActive(false);
                finishBtn.SetActive(true);
                receiveBtn.SetActive(false);
                break;
            case State.Receive:
                goBtn.SetActive(false);
                finishBtn.SetActive(false);
                receiveBtn.SetActive(true);
                break;
        }
    }

    void SetReciveState()
    {
        goBtn.SetActive(false);
        finishBtn.SetActive(false);
        receiveBtn.SetActive(true);
    }

    public void GoBtnClick()
    {
        //您当前位于监狱中，不能进行该操作
        if (StaticData.isPlayerNeedJail)
        {
            BoxManager.CreatePopupTextBox(TableTextBlo.GetText("prison_you_cannot_do"));
            return;
        }
        PageManager.Instance.GetDictPage("AchievementPage").Close();

        //突破等级判断
        if (pageName == "characterRemotePage" && StaticData.playerData.level < 35)
        {
            BoxManager.CreatePopupTextBox(TableTextBlo.GetText("role_level_lack"));
            return;
        }
        PageManager.Instance.OpenPage(pageName,option);
    }

    public void  UpdatePageInfo()
    {
      AchievementPage page =  (AchievementPage)PageManager.Instance.GetDictPage("AchievementPage");
        foreach(var item in page.grid.childs)
        {
            Destroy(item);
        }
        page.grid.childs.Clear();
        page.CreateAchievementList();
        m_update_red_point();
    }
    public  void RewardBtnClick()
    {
        CSGetAchievementReward msg = new CSGetAchievementReward();
        msg.id = rewardID;
        NetAPI.SendHttp(OpDefine.CSGetAchievementReward, msg, (obj, ok) =>
            { 
                if(ok)
                {
                    SCGetAchievementReward result = obj.kBody as SCGetAchievementReward;
                    //item_202 特殊处理 （result为空时）
                    if (result.get.simps.Count == 0 && result.get.treas.Count == 0 && result.get.equips.Count == 0 && result.get.treas.Count == 0 )
                    {
                        BoxManager.CreatePopupTextBox(TableTextBlo.GetText("expedition_rewards_ok"));
                    }
                    else
                    {
                        StaticData.playerData.AddResultObj(result.get);
                        CommonMethod.ShowGetObjs(result.get, "");
                    }
                    SetReciveState();
                    Invoke("UpdatePageInfo", 0.3f);
                }                
            });
    }

    #region 各种入口
    void GetItemConfig(int type)
    {
        switch ((Type)type)
        {

            case Type.VIP:
                titleDesc = "achievement_vip";
                pageName = "ChargePage";
                break;

            case Type.Fuben:
                titleDesc = "achievement_fuben_start_1";
                pageName = "UICopy";
                break;

            case Type.Fuben_2:
                titleDesc = "achievement_fuben_start_2";
                pageName = "UICopy";
                break;

            case Type.PlyerLevel:
                titleDesc = "achievenment_player_level_1";
                pageName = "UICopy";
                break;

            case Type.PlayerLevel_2:
                titleDesc = "achievenment_player_level_2";
                pageName = "UICopy";
                break;

            case Type.Fight:
                titleDesc = "achievement_fight";
                pageName = "ArrayPage";
                break;

            case Type.Equip:
                titleDesc = "achievement_equip_4";
                pageName = "ArrayPage";
                break;

            case Type.Baodong:
                titleDesc = "achievement_baodong";
                pageName = "HelpFriendsPage";
                break;
            //限时优惠 TODO
            case Type.Buy:
                titleDesc = "achievement_reset";
                pageName = "UIActivity";
                break;

            case Type.Reset:
                titleDesc = "achievement_reset";
                pageName = "UIActivity";
                break;

            case Type.TBC:
                titleDesc = "achievement_chuzheng";
                pageName = "MercenaryTaskPage";
                option = "tab=0";
                break;
            //StaticData.playerData.heroWarUID
            case Type.ShiLian:
                titleDesc = "achievement_shilian";
                pageName = "RushPage";
                break;

                //TODO  
            case Type.TuPo:
                titleDesc = "achievement_tupo";
                pageName = "characterRemotePage";
                option = "guid=" + StaticData.playerData.InstPlayerData.GetBattleCamp(1).hero.guid + "&tab=3";
                break;

            case Type.Shangzhen:
                titleDesc = "achievement_shangzhen";
                pageName = "ArrayPage";
                break;

            case Type.Baowu:
                titleDesc = "achievement_baowu";
                pageName = "ArrayPage";
                break;

            case Type.PanJun:
                titleDesc = "achievement_kill_rebel";
                pageName = "MercenaryTaskPage";
                option = "tab=1";
                break;
        }
    }
    #endregion

}